package com.loooooo.pm2024.web.session;

import com.loooooo.pm2024.MyApplication;
import com.loooooo.pm2024.config.RedisUtils;
import com.loooooo.pm2024.modules.secu.LoginedUserHolder;
import com.loooooo.cas.web.session.MyHttpSession;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class MySessionFilter implements Filter {
    private Map<String, MyHttpSession> sessionMap = new HashMap<>();

    RedisUtils redisUtils = MyApplication.getBean(RedisUtils.class);

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        LoginedUserHolder.remove();
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        MyServletRequestWrapper requestWrapper = new MyServletRequestWrapper(request);
        SessionContext sc = new SessionContext();
        sc.setRequest(requestWrapper);
        sc.setResponse((HttpServletResponse) servletResponse);
        SessionContextHolder.put(sc);
        HttpSession session = SessionUtils.getSession(true);
        session.setAttribute("test",System.currentTimeMillis());
        requestWrapper.setSession(session);
        filterChain.doFilter(requestWrapper, servletResponse);
        SessionUtils.saveSession();
        LoginedUserHolder.remove();
    }


}